package com.bing.bingPro.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.controller.dto.MgDamTableRelationDTO;
import com.bing.bingPro.controller.vo.OltDailyFaultDetailVO;
import com.bing.bingPro.entity.TSgccPowerCutNotice;
import com.bing.bingPro.entity.TSgccPowerCutNoticeAll;
import com.bing.bingPro.mapper.OltDailyFaultDetailMapper;
import com.bing.bingPro.mapper.TSgccPowerCutNoticeMapper;
import com.bing.bingPro.service.TSgccPowerCutNoticeAllService;
import com.bing.bingPro.service.TSgccPowerCutNoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author 29821
 * @description 针对表【t_sgcc_power_cut_notice】的数据库操作Service实现
 * @createDate 2024-04-07 16:25:05
 */
@Service
public class TSgccPowerCutNoticeServiceImpl extends ServiceImpl<TSgccPowerCutNoticeMapper, TSgccPowerCutNotice>
        implements TSgccPowerCutNoticeService {

    @Resource
    private OltDailyFaultDetailMapper oltDailyFaultDetailMapper;

    @Resource
    private TSgccPowerCutNoticeMapper tSgccPowerCutNoticeMapper;

    @Resource
    private TSgccPowerCutNoticeAllService tSgccPowerCutNoticeAllService;

    @Override
    public List<TSgccPowerCutNoticeAll> list(MgDamTableRelationDTO infoDTO) {
        List<TSgccPowerCutNoticeAll> faultDetails = oltDailyFaultDetailMapper.selectList();
        List<TSgccPowerCutNotice> powerCutNotices = tSgccPowerCutNoticeMapper.list();
        Map<String, List<TSgccPowerCutNoticeAll>> faultDetailMap = new HashMap<>();
        for (TSgccPowerCutNoticeAll detail : faultDetails) {
            String key = detail.getProvince() + detail.getCityName() +
                    (detail.getRegionName().length() > 2 ? detail.getRegionName().substring(0, detail.getRegionName().length() - 1) : detail.getRegionName());
            faultDetailMap.computeIfAbsent(key, k -> new ArrayList<>()).add(detail);
        }
        Set<TSgccPowerCutNoticeAll> filteredNotices = new HashSet<>();
        Set<TSgccPowerCutNoticeAll> unmatchedDetails = new HashSet<>(faultDetails);

        for (TSgccPowerCutNotice notice : powerCutNotices) {
            String provinceName = notice.getProvinceName().replace("省", "");
            String cityName = notice.getCityName();
            String modifiedRegionName = notice.getRegionName();
            String key = provinceName + cityName + modifiedRegionName;
            List<TSgccPowerCutNoticeAll> matchedDetails = faultDetailMap.getOrDefault(key, Collections.emptyList());

            for (TSgccPowerCutNoticeAll detail : matchedDetails) {
                LocalDateTime start = LocalDateTime.parse(notice.getStartTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                LocalDateTime end = LocalDateTime.parse(notice.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
               /* LocalDateTime faultStart = detail.getOltFaultStartTime();
                LocalDateTime faultEnd = detail.getOltFaultEndTime();

                if (!faultStart.isBefore(start) && !faultEnd.isAfter(end)) {
                    filteredNotices.add(detail);
                    unmatchedDetails.remove(detail);
                }*/
            }
        }

        // 插入未匹配到的数据
        unmatchedDetails.forEach(detailVO -> {
            detailVO.setStats("0");
            tSgccPowerCutNoticeAllService.getBaseMapper().insert(detailVO);
        });

        // 插入筛选出的数据
        List<TSgccPowerCutNoticeAll> filteredNoticesList = new ArrayList<>(filteredNotices);
        for (TSgccPowerCutNoticeAll notice : filteredNoticesList) {
            notice.setStats("1");
            tSgccPowerCutNoticeAllService.getBaseMapper().insert(notice);
        }

        System.out.println("数据处理及转换插入完成");

        return filteredNoticesList;
    }


    private TSgccPowerCutNoticeAll convertToTSgccPowerCutNoticeAll(TSgccPowerCutNoticeAll notice) {
        TSgccPowerCutNoticeAll noticeAll = new TSgccPowerCutNoticeAll();
        noticeAll.setProvince(notice.getProvince());
        noticeAll.setCityName(notice.getCityName());
        noticeAll.setRegionName(notice.getRegionName());
        noticeAll.setOltFaultStartTime(notice.getOltFaultStartTime());
        noticeAll.setOltFaultEndTime(notice.getOltFaultEndTime());
        noticeAll.setDt(notice.getDt());
        noticeAll.setOltFaultOltIp(notice.getOltFaultOltIp());
        noticeAll.setOltOosDuration(notice.getOltOosDuration());
        noticeAll.setOltBeforeFaultActiveUsrNum(notice.getOltBeforeFaultActiveUsrNum());
        noticeAll.setExcludePowerCutUserNum(notice.getExcludePowerCutUserNum());
        noticeAll.setStats("1");
        return noticeAll;
    }
}




